/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-03-03
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Tag;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.TagService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 标签管理控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-03-03
 */
@Api(description = "标签管理接口")
@RestController
@RequestMapping("/admin/tag")
public class TagAdminController extends BaseController {

    @Autowired
    private TagService tagService;

    @Autowired
    private EnterpriseService enterpriseService;

    /**
     * 分页查询标签管理
     *
     * @return
    */
    @ApiOperation("分页查询标签管理")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "name", value = "标签名称", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "type", value = "类型", dataType = "Long", paramType = "query")
    })
    public CommResult<Tag> list(Long type, String name, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (StringUtils.isNotEmpty(name)){
            params.put("name", name);
        }
        if (type!=null){
            params.put("type", type);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Tag> list = tagService.selectList(params);
        PageResult<Tag> pageResult = new PageResult<Tag>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条标签管理
     *
     * @return
    */
    @ApiOperation("查询单条标签管理")
    @GetMapping("/find/{id}")
    public CommResult<Tag> find(@PathVariable String id) {
        Tag result = tagService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("tag",result);
        return CommResult.success(data);

    }

    /**
     * 保存标签管理
     *
     * @param tag
     * @return
     */
    @ApiOperation("保存标签管理")
    @PostMapping("/save")
    public CommResult<Tag> save(@RequestBody Tag tag) {
        Enterprise enterprise = enterpriseService.getCurrent();
        tag.setEnterpriseId(enterprise.getId());
        tag.setCreateDate(new Date());
        int affectCount = tagService.insertUseGeneratedKeys(tag);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Tag result = tagService.findById(tag.getId());
        return CommResult.success(result);

    }


    /**
     * 批量删除标签管理
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除标签管理", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除标签管理,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Tag> del(@PathVariable String ids) {

        tagService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新标签管理
     *
     * @param tag
     * @return
     */
    @Log(desc = "更新标签管理", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新标签管理")
    @PostMapping("/update")
    public CommResult<Tag> update(@RequestBody Tag tag) {
        int affectCount = tagService.updateByPrimaryKeySelective(tag);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Tag result = tagService.findById(tag.getId());
        return CommResult.success(result);
    }

}
